{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 16 IdVd files\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R1C1_L200.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R1C2.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R1C3.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R1C4.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R2C1.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R2C2.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R2C3.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R2C4.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R3C1.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R3C2.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R3C3.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R3C4.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R4C1.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R4C2.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R4C3.csv\n",
      "Processing file: e:\\Code\\origin\\exp_data\\IdVd_R4C4.csv\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import originpro as op\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "import re\n",
    "import glob\n",
    "\n",
    "def origin_shutdown_exception_hook(exctype, value, traceback):\n",
    "    op.exit()\n",
    "    sys.__excepthook__(exctype, value, traceback)\n",
    "\n",
    "if op and op.oext:\n",
    "    sys.excepthook = origin_shutdown_exception_hook\n",
    "\n",
    "# 打开origin界面\n",
    "if op.oext:\n",
    "    op.set_show(True)\n",
    "\n",
    "# 设置路径\n",
    "working_folder = os.getcwd()\n",
    "csv_folder = os.path.join(os.path.abspath('exp_data'))\n",
    "template_folder = os.path.join(os.path.abspath('template'))\n",
    "template_path = os.path.join(template_folder, 'IdVd.otpu')\n",
    "\n",
    "# 选取数据\n",
    "file_prefix ='IdVd'\n",
    "color_index = ['black', 'red', 'blue', 'green', 'cyan', 'magenta', 'yellow', 'orange', 'purple', 'brown']\n",
    "\n",
    "selected_files = []\n",
    "pattern = os.path.join(csv_folder, f'{file_prefix}*')\n",
    "selected_files.extend(glob.glob(pattern))\n",
    "print(f'Found {len(selected_files)} IdVd files')\n",
    "index = 0\n",
    "\n",
    "for file in selected_files:\n",
    "    print(f'Processing file: {file}')\n",
    "    data = pd.read_csv(file, header=0)\n",
    "\n",
    "    gr = op.new_graph(template=template_path)\n",
    "    gr.name = f'G_{os.path.basename(file)}'\n",
    "\n",
    "    ids_columns = [col for col in data.columns if col.startswith('Ids')]\n",
    "    vds_columns = 'Vds(1)'\n",
    "\n",
    "    wks = op.new_sheet('w', f'{os.path.basename(file)}')\n",
    "    wks.from_list(0, data[vds_columns], lname = 'Vds')\n",
    "\n",
    "    for i, col in enumerate(ids_columns):\n",
    "        wks.from_list(i + 1, data[col], lname = col)\n",
    "\n",
    "    for i, col in enumerate(ids_columns):\n",
    "        p = gr[0].add_plot(wks, colx='Vds', coly=col)\n",
    "        p.color = color_index[i % len(color_index)]\n",
    "\n",
    "    p = gr[0].add_plot(wks, colx='Vds', coly=col)\n",
    "    p.color = color_index[i % len(color_index)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关闭origin界面\n",
    "if op.oext:\n",
    "    op.exit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
